package com.isitandroid.coldfireteamapp;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBarItem;
import greendroid.widget.ActionBarItem.Type;
import greendroid.widget.QuickAction;
import greendroid.widget.QuickActionBar;
import greendroid.widget.QuickActionWidget;
import greendroid.widget.QuickActionWidget.OnQuickActionClickListener;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class FlipperActivity extends GDActivity implements OnGestureListener  {
	
	private static final int SWIPE_MIN_DISTANCE 		= 120; 
	private static final int SWIPE_MAX_OFF_PATH 		= 250; 
	private static final int SWIPE_THRESHOLD_VELOCITY 	= 200;
	
	// pour le menu de l'actionbar
	private final int LOCATE = 0;
	private final int REFRESH = 1;
	private final int SHARE = 2;
	// pour le quickactions
	private QuickActionWidget quickActions;
	
	
	//variable pour le page fliper
	
	private Animation		slideLeftIn;
	private Animation		slideLeftOut;
	private Animation		slideRightIn;
	private Animation		slideRightOut;
	
	private GestureDetector detector;
	private ViewFlipper view ; 
	
	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       

        Tools.initTheToast(this);
        setActionBarContentView(R.layout.mainflipper);
        initActionBar();
        initQuickActionBar();
//        getActionBar().setTitle("test"); 


        
        
        detector = new GestureDetector(this,this);
        view         	= (ViewFlipper)findViewById(R.id.flipper);
        
		slideLeftIn 	= AnimationUtils.loadAnimation(this, R.anim.slide_left_in); 
		slideLeftOut 	= AnimationUtils.loadAnimation(this, R.anim.slide_left_out); 
		slideRightIn 	= AnimationUtils.loadAnimation(this, R.anim.slide_right_in); 
		slideRightOut 	= AnimationUtils.loadAnimation(this, R.anim.slide_right_out);		
    }
    
@Override
public boolean onTouchEvent(MotionEvent event) {
	// TODO Auto-generated method stub
	return detector.onTouchEvent(event);
}
    
	
    private void initActionBar() {
    	addActionBarItem(Type.Locate, LOCATE);
    	addActionBarItem(Type.Refresh, REFRESH);
    	addActionBarItem(Type.Share, SHARE);
    	}
    
    
    @Override
    public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
    	switch (item.getItemId()) {
        case LOCATE:
        	Tools.showTheToast("hello world ");
            break;
     
        case REFRESH:
            Toast.makeText(getApplicationContext(),
                "Vous avez cliquez sur le bouton REFRESH",
                Toast.LENGTH_SHORT).show();
            break;
     
        case SHARE:
        	quickActions.show(item.getItemView());
            Toast.makeText(getApplicationContext(),
                "Vous avez cliquez sur le bouton SHARE",
                Toast.LENGTH_SHORT).show();
            break;
        default:
            return super.onHandleActionBarItemClick(item, position);
        }
     
        return true;
    }
    
    private void initQuickActionBar() {
        quickActions = new QuickActionBar(this);
        quickActions.addQuickAction(new QuickAction(this, R.drawable.facebook,
            "Facebook"));
        quickActions.addQuickAction(new QuickAction(this, R.drawable.twitter, "Twitter"));
        quickActions.addQuickAction(new QuickAction(this, R.drawable.blogblogs, "Email"));
        quickActions.setOnQuickActionClickListener(new OnQuickActionClickListener() {
            public void onQuickActionClicked(QuickActionWidget widget,
                int position) {
             Toast.makeText(FlipperActivity.this,
             "Item " + position + " clicked", Toast.LENGTH_SHORT)
             .show();
            }
        });
        }

    
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; 
		if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
		  && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
			view.setInAnimation(slideLeftIn); 
			view.setOutAnimation(slideLeftOut); 
			view.showNext(); 
		}else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
			     && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
			view.setInAnimation(slideRightIn); 
			view.setOutAnimation(slideRightOut); 
			view.showPrevious(); 
		} 		
		return false;
	}


	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public void onLongPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}




	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		return false;
	}




	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}




	public boolean onSingleTapUp(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}
    
}